//
//  RootViewController.m
//  EditTableViewExample
//

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"Countries";
	arrCountries = [[NSMutableArray alloc] initWithObjects:
					@"Australia",
					@"Canada",
					@"Germany",
					@"France",
					@"Great Britain",
					@"Italy",
					@"Japan",
					@"New Zealand",
					@"South Korea",
					@"United States", nil];
	arrTemp = [arrCountries mutableCopy];
	
	CGRect tableFrame = [self.tableView frame];
	CGRect searchRect = tableFrame;
	searchRect.size.height = 40;
	search = [[UISearchBar alloc] initWithFrame:searchRect];
	search.delegate = self;
	self.tableView.tableHeaderView = search;
	
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	searchDoneButton = [[UIBarButtonItem alloc]
						initWithBarButtonSystemItem:UIBarButtonSystemItemDone
						target:self
						action:@selector(searchDone:)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arrCountries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	cell.textLabel.text = [arrCountries objectAtIndex:indexPath.row];
	NSString *imageName = [NSString stringWithFormat:@"%@.png", [arrCountries objectAtIndex:indexPath.row]];
	cell.imageView.image = [UIImage imageNamed:imageName];	

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
	  toIndexPath:(NSIndexPath *)toIndexPath {
	NSString *name = [arrCountries objectAtIndex:fromIndexPath.row];
	[arrCountries removeObjectAtIndex:fromIndexPath.row];
	[arrCountries insertObject:name atIndex:toIndexPath.row];
}

- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		[arrTemp removeObject:[arrCountries objectAtIndex:indexPath.row]];
		[arrCountries removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
						 withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }   
}

- (void)searchDone:(id)sender {
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
	[search resignFirstResponder];
}

- (void)doSearch {
	NSString *searchFor = search.text;
	
	[arrCountries release];
	arrCountries = [arrTemp mutableCopy];
	
	if ([searchFor length] > 0) {
		NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchFor];
		[arrCountries filterUsingPredicate:pred];
	}
	
	[self.tableView reloadData];
}

- (void)searchBarTextDidBeginEditing:(id)sender {
	self.navigationItem.rightBarButtonItem = searchDoneButton;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
	[self doSearch];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
	[self searchDone:nil];
}

- (void)dealloc {

	[arrCountries release];
	[arrTemp release];
	[search release];
	[searchDoneButton release];

    [super dealloc];
}

@end
